package com.itheima.datastructure.queue;

import java.util.Iterator;

/*
ArratQueue1有一个缺点：tail指针不能存储元素
 */
public class ArrayQueue2 <E> implements Queue<E>,Iterable<E>{
    //定义环形数组
    private E[] array;
    private int head=0;//头指针
    private int tail=0;//尾指针
    private int size=0;//元素个数

    @SuppressWarnings("all")
    public ArrayQueue2(int capacity){
        //为了判断是否已经满了，要空一位
        array=(E[]) new Object[capacity];
    }
    @Override
    public boolean offer(E value) {
        if(isFull()){
            return false;
        }
        array[tail]=value;
        tail=(tail+1)%array.length;
        size++;
        return true;
    }

    @Override
    public E poll() {
        if(isEmpty()){
            return null;
        }
        E value=array[head];
        head=(head+1)% array.length;
        size--;
        return value;
    }

    @Override
    public E peek() {
        if(isEmpty()){
            return null;
        }
        return array[head];
    }

    @Override
    public boolean isEmpty() {
        return size==0;
    }

    @Override
    public boolean isFull() {
        return size== array.length;
    }

    @Override
    public Iterator<E> iterator() {
        return new Iterator<E>() {
            int p = head;
            int count=0;

            @Override
            public boolean hasNext() {
                return count<size;
            }

            @Override
            public E next() {
                E value = array[p];
                p = (p + 1) % array.length;
                count++;
                return value;
            }
        };
    }
}
